import React from "react";
import { Navigate } from "react-router-dom";
const Home = React.lazy(() => import("../views/home"));
const Login = React.lazy(() => import("../views/login"));
const AddressList = React.lazy(() => import("../views/addressList"));
const AddressListAdd = React.lazy(() => import("../views/addressListAdd"));
const AddressListEdit = React.lazy(() => import("../views/addressListEdit"));

const PrivateRoute = ({ children }: { children: JSX.Element }) => {
  const token = localStorage.getItem("token");
  if (token) {
    return children;
  } else {
    return <Navigate to="/login"></Navigate>;
  }
};

export const routes = [
  {
    path: "/",
    element: <Home></Home>,
  },
  {
    path: "/login",
    element: <Login></Login>,
  },
  {
    path: "/address/list",
    element: (
      <PrivateRoute>
        <AddressList></AddressList>
      </PrivateRoute>
    ),
  },
  {
    path: "/address/list/add",
    element: <AddressListAdd></AddressListAdd>,
  },
  {
    path: "/address/list/:id/edit",
    element: <AddressListEdit></AddressListEdit>,
  },
];
